package com.simple.handler.demo003;


import com.simple.handler.demo003.dao.GetewayDao;
import com.simple.handler.demo003.dao.GetewayDaoImpl;
import com.simple.handler.demo003.entities.GetewayEntity;

/**
 * 网关责任链工厂 设置请求链
 */
public class GetewayHandlerEnumFactory {

    private static GetewayDao getewayDao = new GetewayDaoImpl();

    public static GetewayHandler getFirstGetewayHandler(){
        // 1. 获取第一处理者 那么那个是第一个处理者呢 prehandlerId == null 的就是第一个handler
        GetewayEntity firstGetewayEntity = getewayDao.getFirstGetewayEntity();
        GetewayHandler firstGetewayHandler = newGetewayHandler(firstGetewayEntity);

        //获取到第一个handler的下一个handlerId
        GetewayEntity tempGetewayEntity = firstGetewayEntity;
        Integer nexthandlerId = null;
        GetewayHandler tempGetewayHandler = firstGetewayHandler;
        while((nexthandlerId = tempGetewayEntity.getNexthandlerId()) != null){
            GetewayEntity getewayEntity = getewayDao.getGetewayEntity(nexthandlerId);
            GetewayHandler getewayHandler = newGetewayHandler(getewayEntity);
            tempGetewayHandler.setNext(getewayHandler);
            tempGetewayHandler = getewayHandler;
            tempGetewayEntity = getewayEntity;
        }

        return firstGetewayHandler;
    }


    public static GetewayHandler getFirstGetewayHandler1(){
        // 1. 获取第一处理者 那么那个是第一个处理者呢 prehandlerId == null 的就是第一个handler
        GetewayEntity firstGetewayEntity = getewayDao.getFirstGetewayEntity();
        GetewayHandler firstGetewayHandler = newGetewayHandler(firstGetewayEntity);

        if(firstGetewayHandler == null){
            return null;
        }
        setNextGetewayHandler(firstGetewayEntity,firstGetewayHandler);

        return firstGetewayHandler;
    }


    private static void setNextGetewayHandler(GetewayEntity getewayEntity,GetewayHandler getewayHandler){
        if(getewayHandler != null && getewayEntity != null){
            Integer nexthandlerId = getewayEntity.getNexthandlerId();
            GetewayEntity nextGetewayEntity = getewayDao.getGetewayEntity(nexthandlerId);
            GetewayHandler nextGetewayHandler = newGetewayHandler(getewayEntity);
            getewayHandler.setNext(nextGetewayHandler);
            //递归
            setNextGetewayHandler(nextGetewayEntity,nextGetewayHandler);
        }
    }








    /**
     * 反射实体化具体的处理者
     * @param getewayEntity
     * @return
     */
    private static GetewayHandler newGetewayHandler(GetewayEntity getewayEntity){
        if(getewayEntity == null){
            return null;
        }
        String handlerClassName = getewayEntity.getHandlerClassName();
        try {
            Class<?> clazz = Class.forName(handlerClassName);
            return (GetewayHandler) clazz.newInstance();
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (InstantiationException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        }
        return null;
    }
}
